clear all
set more off
set matsize 10000
tempfile ubigeo temp

local controls c.schooling male rural native married head 

use mergedp, clear

egen voted_bn = rowmax(voted_blank voted_null)
recode voted_blank voted_null voted_bn (. = 0) if voted == 0

** LABELS
label var todoAT "Violence Exposure"

******* Results
drop if vote_matter == .
local lista vote_matter voted voted_blank voted_null voted_bn partN dem_works dem_matters1 gov_demo1 gov_auto1

gen depnac1993 = int(provnac1993/100)
gen dep1993 = int(prov1993/100)

gen migration_dist = ubigeonac1993 != ubigeo1993 
gen migration_prov = provnac1993 != prov1993
gen migration_dep = depnac1993 != dep1993

gen dist_prov = migration_dist == 1 & migration_prov == 0
gen dist_dep = migration_dist == 1 & migration_prov == 1 & migration_dep == 0 

egen grupo = group(ubigeo1993 ubigeonac1993)

local r = 1
foreach geo in dist prov dep {
		
	local betam_`geo' ""
	local betan_`geo' ""
	local sem_`geo' ""
	local sen_`geo' ""
	local dif_`geo' ""

	if "`geo'" == "dist" {
		local abs 
		local reg reg
		local rest
	}
	else if "`geo'" == "prov" {
		local abs 
		local reg reg
		local rest "if dist_prov == 0" 
	}	
	else if "`geo'" == "dep" {
		local abs 
		local reg reg
		local rest "if dist_dep == 0" 
	}	

	local c = 1
	foreach var in `lista' {
	
		replace `var' = `var' * 100 if "`var'" != "partN"
				
		`reg' `var' i.migration_`geo' (`controls' i.age i.provid#c.trend i.year c.todoAT)#i.migration_`geo' `rest', `abs' vce(cluster ubigeonac1993) 
			
		local bm : di %12.3fc _b[c.todoAT#0.migration_`geo'] 
		local sm : di %12.3fc _se[c.todoAT#0.migration_`geo'] 
		local p =  ttail(e(df_r),abs(_b[c.todoAT#0.migration_`geo'] / _se[c.todoAT#0.migration_`geo']))*2
		local stm = cond(`p' <.01,"***",cond(`p' <.05,"**",cond(`p' <.1,"*","")))	
		
		local bn : di %12.3fc _b[c.todoAT#1.migration_`geo'] 
		local sn : di %12.3fc _se[c.todoAT#1.migration_`geo'] 
		local p =  ttail(e(df_r),abs(_b[c.todoAT#1.migration_`geo'] / _se[c.todoAT#1.migration_`geo']))*2
		local stn = cond(`p' <.01,"***",cond(`p' <.05,"**",cond(`p' <.1,"*","")))	
		
		lincom _b[c.todoAT#0.migration_`geo'] - _b[c.todoAT#1.migration_`geo']
		local dif : di %12.3fc r(estimate) 
		local p =  ttail(r(df),abs(r(estimate)/r(se)))*2
		local star = cond(`p' <.01,"***",cond(`p' <.05,"**",cond(`p' <.1,"*","")))	
		
		local ++c
		
		local betam_`geo' =  "`betam_`geo'' & `bm' `stm'"
		local betan_`geo' =  "`betan_`geo'' & `bn' `stn'"
		local sem_`geo' =  "`sem_`geo'' & (`sm')"
		local sen_`geo' =  "`sen_`geo'' & (`sn')"
		local dif_`geo' = "`dif_`geo'' & `dif' `star' "				
		
		replace `var' = `var' / 100 if "`var'" != "partN"

	}	
	di "`sem_`geo''"
	di "`sen_`geo''"
}

texdoc init Table_A2.tex, replace 
tex \begin{table}[h]
tex \caption{Baseline Impacts by Migration Status}
tex \label{tab:Table_A2}
tex \begin{center}
tex \resizebox{16cm}{!}{
tex \begin{tabular}{lcccccccccc} \hline \hline
tex \\
tex & Voting & \multicolumn{4}{c}{Voted in last elections}& Participation & \multicolumn{2}{c}{Democracy} & \multicolumn{2}{c}{Type of preferred gov.} \\
tex & matters & Yes & ...blank & ...null & ... blank or null & organisations & works & matters & Democracy & Autocracy \\
tex & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) \\
tex \\
tex \hline
tex \multicolumn{7}{l}{\textbf{Moved District}} \\
tex Migrants `betam_dist' \\
tex           `sem_dist' \\
tex Non-Migrants `betan_dist' \\
tex          `sen_dist' \\
tex \\
tex Difference `dif_dist' \\
tex \\
tex \multicolumn{7}{l}{\textbf{Moved District and Province}} \\
tex Migrants `betam_prov' \\
tex           `sem_prov' \\
tex Non-Migrants `betan_prov' \\
tex          `sen_prov' \\
tex \\
tex Difference `dif_prov' \\
tex \\
tex \multicolumn{7}{l}{\textbf{Moved District and Region}} \\
tex Migrants `betam_dep' \\
tex           `sem_dep' \\
tex Non-Migrants `betan_dep' \\
tex          `sen_dep' \\
tex \\
tex Difference `dif_dep' \\
tex \\
tex \hline
tex \end{tabular}}
tex \end{center}
tex \end{table}
